package org.lw.ssm.controller;

import org.lw.ssm.Entity.Employee;
import org.lw.ssm.response.ApiResponse;
import org.lw.ssm.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employees")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    // 获取所有员工
    @GetMapping
    public ResponseEntity<ApiResponse<List<Employee>>> getAllEmployees() {
        List<Employee> employees = employeeService.getAllEmployees();

        return ResponseEntity.ok().body(new ApiResponse<>(200,"所有员工信息",employees)) ;
    }

    // 根据ID获取员工
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Employee>> getEmployeeById(@PathVariable("id") Long id) {

        return ResponseEntity.ok().body(new ApiResponse<>(200,"员工信息",employeeService.getEmployeeById(id)));
    }
    //根据部门ID获取员工
    @GetMapping("/dept/{id}")
    public ResponseEntity<ApiResponse<List<Employee>>> getEmployeeByDeptId(@PathVariable("id") Long id) {
        return ResponseEntity.ok().body(new ApiResponse<>(200,"员工信息",employeeService.getEmployeeByDeptId(id)));
    }

    // 添加员工
    @PostMapping
    public void addEmployee(@RequestBody Employee employee) {
        employeeService.insertEmployee(employee);
    }

    // 更新员工信息
    @PutMapping("/{id}")
    public void updateEmployee(@PathVariable Long id, @RequestBody Employee employee) {
        employee.setId(id);
        employeeService.updateEmployee(employee);
    }

    // 删除员工
    @DeleteMapping("/{id}")
    public void deleteEmployee(@PathVariable Long id) {
        employeeService.deleteEmployee(id);
    }
}

